<template>
  <div id="app">
    <div>
      名称: <input v-model.trim="member.name" type="text">
      编号: <input v-model.lazy.trim.number="member.id" type="number">
      <button @click="addList"> 生成</button>
    </div>
    <table class="tb">
      <tr>
        <th>编号</th>
        <th>品牌名称</th>
        <th>创立时间</th>
        <th>操作</th>
      </tr>
      <!-- 循环渲染的元素tr -->
      <tr v-for="(item,index) in list" :key="index">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.time}}</td>
        <td>
          <button @click="delList(index)">删除</button>
        </td>
      </tr>
      <tr>
        <td v-show="list.length === 0" colspan="4">没有数据咯~</td>
      </tr>
    </table>
  </div>
</template>

<script>
var moment = require('moment')
moment().format()
export default {
  data () {
    return {
      list: [
        { id: 1, name: '奔驰', time: '2020-08-01' },
        { id: 2, name: '宝马', time: '2020-08-02' },
        { id: 3, name: '奥迪', time: '2020-08-03' }
      ],
      member: {
        name: '',
        id: ''
      }
    }
  },
  methods: {
    delList (n) {
      this.list.splice(n, 1)
    },
    addList () {
      if (this.member.name === '' || this.member.id === '') return alert('数据不能为空')
      const obj = { ...this.member }
      obj.time = moment().format('L')
      this.list.push(obj)
      this.member.name = ''
      this.member.id = ''
    }
  }
}
</script>

<style>
#app {
  width: 600px;
  margin: 10px auto;
}

.tb {
  border-collapse: collapse;
  width: 100%;
}

.tb th {
  background-color: #0094ff;
  color: white;
}

.tb td,
.tb th {
  padding: 5px;
  border: 1px solid black;
  text-align: center;
}

.add {
  padding: 5px;
  border: 1px solid black;
  margin-bottom: 10px;
}
</style>
